<?php

namespace App\Http\Middleware;

use App\Library\Rms\RmsUserApi;
use App\Remote\Rms\RmsUserRemote;
use App\Service\OpenShop\User\RmsUserService;
use Closure;
use Illuminate\Http\Request;

class MerchantManager
{
    /**
     * @param Request $request
     * @param Closure $next
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $shopId      = (int)$request->header('shopId');
        $managerUids = array_column(RmsUserRemote::getShopManagerUserByShopId([$shopId])['lists'] ?? [], 'userId');

        if (in_array(getRmsUserId(), $managerUids)) {
            return $next($request);
        }

        return response()->json(['code' => 500, 'msg' => '无店长权限']);
    }
}
